﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevEx.Thanh.Class;
using DevExpress.Utils;

namespace DevEx.QuocSy.UserControl
{
    public partial class ucQuanLyLoHang : DevExpress.XtraEditors.XtraUserControl
    {
        private CTonController ctrlTon = new CTonController();
        public QLVTDataContext dc = new QLVTDataContext();

        public ucQuanLyLoHang()
        {
            InitializeComponent();
        }

        private void ucQuanLyLoHang_Load(object sender, EventArgs e)
        {
            //lay du lieu kho
            CViewer.BindKhoToLookupEdit(lkeKho);
        }

        private void lkeKho_EditValueChanged(object sender, EventArgs e)
        {
            int soMatHang = 0;
            //hiển thị danh sách vật tư
            try
            {
                int maKho = Convert.ToInt32(lkeKho.EditValue);
                dtgVatTu.DataSource = ctrlTon.GetTonKho2(maKho);
                FormatdtgVatTu();
            }
            catch (Exception)
            {
                dtgVatTu.DataSource = null;
            }

            //lấy số lượng
            soMatHang = gridView1.RowCount;
            lblTongMatHang.Text = "Hiện có: " + soMatHang.ToString() + " mặt hàng";
        }

        public void FormatdtgVatTu()
        {
            gridView1.Columns["ID_MatHang"].Visible = false;
            gridView1.Columns["TangKeKhoang"].Visible = false;                                    
            
            var cl_VT = gridView1.Columns.ColumnByFieldName("VatTu");
            cl_VT.Width = 150;
            cl_VT.Caption = "Vật Tư";
            cl_VT.OptionsColumn.AllowEdit = false;
            cl_VT.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Near;
            cl_VT.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //Columns MaKyHieu
            var cl_MKH = gridView1.Columns.ColumnByFieldName("MaKyHieu");
            cl_MKH.Width = 120;
            cl_MKH.Caption = "Mã Ký Hiệu";
            cl_MKH.OptionsColumn.AllowEdit = false;
            cl_MKH.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Near;
            cl_MKH.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //Columns SoTheKho
            var cl_SoTheKho = gridView1.Columns.ColumnByFieldName("SoTheKho");
            cl_SoTheKho.Width = 80;
            cl_SoTheKho.Caption = "Thẻ Kho";
            cl_SoTheKho.OptionsColumn.AllowEdit = false;
            cl_SoTheKho.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Near;
            cl_SoTheKho.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;  
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            // focus row filter
            if (e.FocusedRowHandle <0)
                return;
            
            //xoa trang thong tin
            foreach (Control c in panelThongTinVatTu.Controls)
            {
                if (c.GetType() == typeof(TextEdit))
                {
                    TextEdit text = (TextEdit)c;
                    text.EditValue = "";
                    text.Properties.ReadOnly = true;
                }
            }
            dtgPhieuNhapXuat.DataSource = null;
            //lấy thông tin mã mặt hàng
            int idMatHang = (int)gridView1.GetFocusedRowCellValue("ID_MatHang");
            //lấy thông tin tồn
            tblTon curMatHang = ctrlTon.SelectRecordById(idMatHang);
            if (curMatHang != null)
            {
                //hiển thị thông tin chi tiết
                txtTenVT.EditValue = curMatHang.tblVatTu.VatTu;
                txtKyMaHieu.EditValue = curMatHang.tblVatTu.MaKyHieu;
                if (curMatHang.Gia.HasValue)
                {
                    txtGiaLe.EditValue = curMatHang.Gia.Value.ToString("#,##0.##");
                }
                txtDVT.EditValue = curMatHang.tblVatTu.tblDVT.DVT;
                txtCCL.EditValue = curMatHang.tblCCL.CCL;
                txtKho.EditValue = curMatHang.tblKho.Kho;
                txtNamSX.EditValue = curMatHang.NamSX;
                txtNgayTaoTon.EditValue = curMatHang.NgayTaoTon.Value.ToString("dd/MM/yyyy");
                txtNhapKH.EditValue = curMatHang.SLNhapKH.ToString();
                txtNhapTH.EditValue = curMatHang.SLNhapTH;
                txtTonDau.EditValue = curMatHang.SLSS11;
                txtTonDauNam.EditValue = curMatHang.SLKK11;
                txtTonKH.EditValue = curMatHang.SLTonKH;
                txtTonTH.EditValue = curMatHang.SLTonTH;
                txtXuatKH.EditValue = curMatHang.SLXuatKH;
                txtXuatTH.EditValue = curMatHang.SLXuatTH;

                txtSoTheKho.EditValue = curMatHang.SoTheKho + "-" + curMatHang.Code.ToString();
                txtViTri.EditValue = curMatHang.TangKeKhoang;
                if (curMatHang.ID_HopDong != null)
                    txtNguonGoc.EditValue = curMatHang.tblHopDong.SoHopDong;
                else
                    txtNguonGoc.EditValue = "";

                //hiển thị thông tin nhập xuất của mặt hàng
                //gridTTNhapXuat.DataSource = ctrlTon.GetTTNhapXuat(curMatHang.ID_MatHang);
                KhoiTaodtgPhieuNhapXuat(curMatHang.ID_MatHang);
            }
        }

        public void KhoiTaodtgPhieuNhapXuat(int ID_MatHang)
        {
            var q = from p in dc.tblDPNXes
                    join k in dc.tblSLNXes on p.ID_DPNX equals k.ID_DPNX
                    where k.ID_MatHang  == ID_MatHang
                    select new { p.ID_DPNX , 
                                 NX = p.NX == "N" ? " Phiếu Nhập" : " Phiếu Xuất",
                                 SoPhieu = (Convert.ToDateTime(p.NgayPhieu).Month > 9 ?
                                 Convert.ToString(p.SoPhieu) + "/" + Convert.ToDateTime(p.NgayPhieu).Month.ToString() + Convert.ToDateTime(p.NgayPhieu).Year.ToString().Substring(2, 2)
                                 : Convert.ToString(p.SoPhieu) + "/0" + Convert.ToDateTime(p.NgayPhieu).Month.ToString() + Convert.ToDateTime(p.NgayPhieu).Year.ToString().Substring(2, 2)),
                                 p.NgayPhieu,
                                 k.SLKH,
                                 p.tblDonVi.DonVi
                    };

            dtgPhieuNhapXuat.DataSource = q;
            FormatdtgPhieuNhapXuat();
        }

        public void FormatdtgPhieuNhapXuat()
        {
            gridView2.Columns["ID_DPNX"].Visible = false;
            //gridView2.Columns["NX"].OptionsColumn.AllowGroup = DefaultBoolean.True ;//.Visible = false;

            gridView2.Columns["NX"].Group();
            gridView2.ExpandAllGroups();
            gridView2.Columns["NX"].AppearanceCell.ForeColor = Color.Blue;
            //gridView2.Columns["NX"].GroupFormat.FormatString.;


            var cl_VT = gridView2.Columns.ColumnByFieldName("SoPhieu");
            cl_VT.Width = 80;
            cl_VT.Caption = "Số Phiếu";
            cl_VT.OptionsColumn.AllowEdit = false;
            cl_VT.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            cl_VT.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //Columns NgayPhieu
            var cl_NgayPhieu = gridView2.Columns.ColumnByFieldName("NgayPhieu");
            cl_NgayPhieu.Width = 80;
            cl_NgayPhieu.Caption = "Ngày Phiếu";
            cl_NgayPhieu.OptionsColumn.AllowEdit = false;
            cl_NgayPhieu.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            cl_NgayPhieu.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //Columns DonVi
            var cl_DonVi = gridView2.Columns.ColumnByFieldName("DonVi");
            cl_DonVi.Width = 300;
            cl_DonVi.Caption = "Đơn vị giao nhận";
            cl_DonVi.OptionsColumn.AllowEdit = false;
            cl_DonVi.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Near;
            cl_DonVi.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //Columns SLTonKH
            var cl_SLTonKH = gridView2.Columns.ColumnByFieldName("SLKH");
            cl_SLTonKH.Width = 80;
            cl_SLTonKH.Caption = "SLKH";
            cl_SLTonKH.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Far;
            cl_SLTonKH.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            cl_SLTonKH.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            cl_SLTonKH.DisplayFormat.FormatString = "#,###.##";
            Font f = new Font(this.Font, FontStyle.Bold);
            cl_SLTonKH.AppearanceCell.Font = f;
        }
    }
}
